#!/bin/bash
basefun="`which functions.sh`"
if [ "$basefun" != "" ]; then
	. $basefun
else
	echo -e "cant find functions.sh or parseini.sh\n exit ..."
	exit 1
fi

inputXls=""
outputSh=""

origFrameworkRev=
origAppRev=
origUnknownRev=

trunkRev=
driverRev=
modemRev=
appRev=
frameworkUnknownRev=
appUnknownRev=

repoRootURL=
productPath=
trunkPath=
driverPath=
modemPath=
appRepoRootURL=
appPackagePath=

function Usage()
{
	echo "Usage: get-mainsoft-svn.sh [-h|--help] [-x|--xls] your_filtered_xls_file [-o|--out] output_shell_file";
	exit
}

function FilterFrameAppRev()
{
	local origFile="$1"
	local isRevEmpty=""
	local appR=""
	local fraR=""
	cat $origFile | while read line ;do
		isRevEmpty=""
		appR=""
		fraR=""
		modemR=""
		if [ "$line" != "" ]; then
			engNameBugID="`echo $line | awk -F '=' '{print $1}'`"
			svnInfo="`echo $line | awk -F '=' '{print $2}'`"
			newline="`echo $svnInfo | tr A-Z a-z | awk -F '|' '{print $1" "$2 " "$3}' `"
			for l in $newline; do
				isFramework="`echo $l | grep "fra"`"
				isApp="`echo $l | grep "app"`"
				isModem="`echo $l | grep "modem"`"
				if [ "$isFramework" != "" ]; then
					fraR="`echo $isFramework | sed "s/[^0-9]/ /g"`"
					origFrameworkRev="$origFrameworkRev $fraR"
				elif [ "$isApp" != "" ]; then
					appR="`echo $isApp | sed "s/[^0-9]/ /g"`"
					origAppRev="$origAppRev $appR"
				elif [ "$isModem" != "" ]; then
					modemR="`echo $isModem | sed "s/[^0-9]/ /g"`"
					origModemRev="$origModemRev $modemR"
				else
					fraR="`echo $l | sed "s/[^0-9]/ /g"`"
					origFrameworkRev="$origFrameworkRev $fraR"
				fi
			done
		fi
		isRevEmpty="`echo "$appR $fraR $modemR" | grep "[0-9]"`"
		if [ "$isRevEmpty" == "" ]; then
			echoTextRed "WARNNING! \"$engNameBugID\"  has no svn revision information!"
            echo "WARNNING! \"$engNameBugID\"  has no svn revision information!">> $outputSh.lost
		fi
		echo "#!/bin/bash
		origFrameworkRev=\"$origFrameworkRev\"
		origAppRev=\"$origAppRev\"
		origModemRev=\"$origModemRev\"
		origUnknownRev=\"$origUnknownRev\"" > /tmp/filterver.sh
	done
}
function IsFrameworkRev(){
	local rev="$1"
	local ret=""
	if [ "`GetSvnLogPath $repoRootURL $rev $trunkPath`" != "" ]; then
		trunkRev="$trunkRev $rev"
		ret="yes"
	elif [ "`GetSvnLogPath $repoRootURL $rev $driverPath`" != "" ]; then
		driverRev="$driverRev $rev"
		ret="yes"
	fi
	if [ "$ret" != "yes" ]; then
		frameworkUnknownRev="$frameworkUnknownRev $rev"
	fi
}
function IsAppRev(){
	local rev="$1"
	if [ "`GetSvnLogPath $appRepoRootURL $rev $appPackagePath`" != "" ]; then
		appRev="$appRev $rev"
	else
		appUnknownRev="$appUnknownRev $v"
	fi
}

function IsModemRev(){
	local rev="$1"
	if [ "`GetSvnLogPath $repoRootURL $rev $modemPath`" != "" ]; then
		modemRev="$modemRev $rev"
	else
		modemUnknownRev="$modemUnknownRev $v"
	fi
}

function DispatchFrameRev()
{
	for v in $origFrameworkRev;do
		IsFrameworkRev $v
	done
}

function DispatchModemRev()
{
	for v in $origModemRev;do
		IsModemRev $v
	done
}


function DispatchAppRev()
{
	for v in $origAppRev; do
		IsAppRev $v
	done
}


while [ $# != 0 ]; do
	case $1 in
		-x|--xls)
			inputXls=$2
			shift
		;;
		-o|--out)
			outputSh=$2
			shift
		;;
		
		-h|--help)
			Usage
			exit
		;;
		*)
			echo "Unknown argument!!,exit ...\n"
			Usage
			exit
		;;
	esac
	shift
done
if [ "$inputXls" == "" ]; then
echo "Miss argument of input xls filename... exit"
	Usage
	exit
fi
if [ "$outputSh" == "" ]; then
  	outputSh="mainsoftResult`date +"%Y%m%d-%H"`.sh"
fi

echo "" > $outputSh.lost #clearup it at first anyway
$FILTER_XLS -n $inputXls -o "filterOut.xls"
$PARSE_XLS "filterOut.xls" $outputSh $tempRawSh
sed -i "s/\s/,/g" $tempRawSh
FilterFrameAppRev $tempRawSh

chmod +x /tmp/filterver.sh
. /tmp/filterver.sh

InitVars

DispatchFrameRev "$origFrameworkRev"
DispatchAppRev "$origAppRev"
DispatchModemRev "origModemRev"


uniqSortTrunkRev="`SortUniqNumber "$trunkRev"`"
uniqSortDriverRev="`SortUniqNumber "$driverRev"`"
uniqSortModemRev="`SortUniqNumber "$modemRev"`"
uniqSortAppRev="`SortUniqNumber "$appRev"`"
#过滤掉产品线打分支前的版本
uniqSortTrunkRev="`filter_creat_frame_branch_version "$uniqSortTrunkRev"`"
uniqSortAppRev="`filter_creat_app_branch_version "$uniqSortAppRev"`"
#过滤掉产品线打分支前的版本
#过滤掉产品线已经合并过的版本
echo "process_log_file ${repoRootURL}/${productPath} ${framework_product_start_version}:HEAD $selProject"
all_framework_product_merged_svn_version="`process_log_file ${repoRootURL}/${productPath} ${framework_product_start_version}:HEAD $selProject`"
echoTextGreen "all_framework_product_merged_svn_version=$all_framework_product_merged_svn_version"
echo "process_log_file ${appRepoRootURL}/${appproductPath} ${app_product_start_version}:HEAD $selProject"
all_app_product_merged_svn_version="`process_log_file ${appRepoRootURL}/${appproductPath} ${app_product_start_version}:HEAD $selProject`"
echoTextGreen "all_app_product_merged_svn_version=$all_app_product_merged_svn_version"

echoTextBlue "framework_merge_svn_version=$uniqSortTrunkRev"
echoTextBlue "app_merge_svn_version=$uniqSortAppRev"

if [ "$uniqSortTrunkRev" = "" ];then
	processed_framework_merge_version=""
else
	processed_framework_merge_version="`filter_product_merged_version $uniqSortTrunkRev $all_framework_product_merged_svn_version`"
fi

if [ "$uniqSortAppRev" = "" ];then
	processed_app_merge_version=""
else
	processed_app_merge_version="`filter_product_merged_version $uniqSortAppRev $all_app_product_merged_svn_version`"
fi

#过滤掉产品线已经合并过的版本

uniqSortFrameworkUnknownRev="`SortUniqNumber "$frameworkUnknownRev"`"
uniqSortAppUnknownRev="`SortUniqNumber "$appUnknownRev"`"
uniqSortModemUnknownRev="`SortUniqNumber "$modemUnknownRev"`"

echo "#!/bin/bash
trunkRev=\"$uniqSortTrunkRev\"
driverRev=\"$uniqSortDriverRev\"
modemRev=\"$uniqSortModemRev\"
appRev=\"$uniqSortAppRev\"
frameworkUnknownRev=\"$uniqSortFrameworkUnknownRev\"
appUnknownRev=\"$uniqSortAppUnknownRev\"
modemUnknownRev=\"$uniqSortModemUnknownRev\"
processed_framework_merge_version=\"$processed_framework_merge_version\"
processed_app_merge_version=\"$processed_app_merge_version\"" >> $outputSh
cat $outputSh >&2

cp $outputSh `dirname $outputSh`/mainsoftResult.sh -rfv
chmod +x `dirname $outputSh`/mainsoftResult.sh
echo "ALL FINISHED. See at $outputSh"
